diag — Diagonalise matrix
diag
has two forms, depending on what type of matrix the first
argument is. If the first argument is a row or column vector, then
diag
returns a square matrix with that vector on a diagonal.
Which diagonal the vector is placed on is determined by the second
argument, which defaults to 0 if you don't specify it. The main
diagonal is taken as 0. The diagonal above the main diagonal are
taken as positive integers, and the diagonals below the main diagonal
are negative integers.
The second form of diag
is used if the first argument is not a
row or column vector. The result of this operation is a column matrix
consisting of all the elements taken from a diagonal. As for the
first form, you can use the optional scalar argument to specify which
diagonal to extract the elements from. The notation is the same, as
is the default of 0.
As an implementation curiosity, if you specify a matrix as the second
argument, then it uses the first element of that matrix to determine
the diagonal to use.
Subsections